package servlet;

import bean.User;
import dao.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(
        name = "testServlet",
        urlPatterns = "test"
)
public class testServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");

        //没错java的switch不支持字符串只能用if (type.equals("login"))
        //        switch (type) {
        //            case "login":
        //                System.out.println("1");
        //                break;
        //            case "other":
        //                System.out.println("2");
        //                break;
        //            default:
        //                break;
        //        }


        req.getQueryString();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        User user = (new UserDAO()).login(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            req.getRequestDispatcher("/User/login").forward(req, resp); //请求转发 登录
        } else {
            resp.sendRedirect("/User"); //302重定向
        }


        super.doGet(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }


}
